﻿using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

namespace Rabbit.Controls
{
    public class TextBlockEx:TextBlock
    {
        private static readonly Regex RE_URL = new Regex(@"#\w+#|@\w+|http://\w*\.(\w+\.\w+|\w+)\S+", RegexOptions.IgnoreCase);
        public new static readonly DependencyProperty TextProperty =
            DependencyProperty.RegisterAttached("Text", typeof(string), typeof(TextBlockEx),
            new PropertyMetadata(null, OnTextChanged));
        public static string GetText(DependencyObject d) { return d.GetValue(TextProperty) as string; }
        public static void SetText(DependencyObject d, string value) { d.SetValue(TextProperty, value); }
        private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var txtBlock = d as TextBlockEx;
            if (txtBlock == null)
                return;
            //clear it's content first
            txtBlock.Inlines.Clear();

            //read its new value
            var newText = (string)e.NewValue;
            if (string.IsNullOrEmpty(newText))
            {
                return;
            }

            // Find all URLs using a regular expression            
            int last_pos = 0;
            foreach (Match match in RE_URL.Matches(newText))
            {
                // Copy raw string from the last position up to the match          
                if (match.Index != last_pos)
                {
                    var raw_text = newText.Substring(last_pos, match.Index - last_pos);
                    txtBlock.Inlines.Add(new Run(raw_text));
                }

                string url = match.Value;
                if (url.ToLower().StartsWith("#"))
                {
                    url = "http://t.sohu.com/ht/" + HttpUtility.UrlEncode(url.Substring(1, url.Length - 2), Encoding.GetEncoding("GB2312"));
                }
                else if (url.ToLower().StartsWith("@"))
                {
                    url = "http://t.sohu.com/n/" + url.Substring(1, url.Length - 1);
                }

                // Create a hyperlink for the match        
                var link = new Hyperlink(new Run(match.Value))
                {
                    CommandParameter = url
                };

                link.Click += OnUrlClick;
                txtBlock.Inlines.Add(link);

                // Update the last matched position         
                last_pos = match.Index + match.Length;
            }

            // Finally, copy the remainder of the string           
            if (last_pos < newText.Length)
                txtBlock.Inlines.Add(new Run(newText.Substring(last_pos)));
        }
        private static void OnUrlClick(object sender, RoutedEventArgs e)
        {
            var link = (Hyperlink)sender;
            Process.Start(link.CommandParameter.ToString());
        }
    }
}
